package youcaihuaoa.weetop.com.vm.main

import android.app.Application
import api.UserServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.configs.AppConstants
import com.robot.base.model.GroupAddBean
import com.robot.base.util.LiveDataBus
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse

class MessageGroupAddViewModel(application: Application) : GLBaseViewModel(application) {

    val uiChangeEvent = UiChangeEvent()

    class UiChangeEvent {
        val dataEvent = SingleLiveEvent<List<GroupAddBean>>()
    }

    fun ryimGetGroupData() {
        showLoadingDialog()
        UserServiceFactory.ryimGetGroupData()
            .subscribe(object :
                AbstractViewModelSubscriber<BaseResponse<List<GroupAddBean>>>(this) {
                override fun onNext(t: BaseResponse<List<GroupAddBean>>) {
                    dismissLoadingDialog()
                    t.data ?: return
                    uiChangeEvent.dataEvent.postValue(t.data)
                }
            })
    }

    fun ryimGroupUserJoin(id: String) {
        showLoadingDialog()
        UserServiceFactory.ryimGroupUserJoin(
            id
        ).subscribe(object :AbstractViewModelSubscriber<BaseResponse<String>>(this){
            override fun onNext(t: BaseResponse<String>) {
                dismissLoadingDialog()
                LiveDataBus.get().with(AppConstants.LiveKey.REFRESH_ADD_GROUP).postValue("1")
                finish()
            }
        })
    }
}